//lockbox.txt - A locked box. Not trapped. Requires a party emmber of Tool Use at least equal to
// lock difficulty to unlock it.
//Cell 0 - Difficulty of lock.
//Cell 1 - The required key. If 0, none. Otherwise, the number of the special item 
//required to open the door.
//Cell 2,3 - The sdf which is set to 1 when box is unlocked.

beginobjectscript; // basic box

variables;
	short i_am_open = 0;
	short i_am_unlocked = 0;

body;

beginstate INIT_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_unlocked = 1;
			}
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE;

break;

beginstate USE_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_unlocked = 1;
		}
		
	if ((get_memory_cell(1) > 0) && (get_memory_cell(0) > 0) && (i_am_unlocked == 0)) {
		if (has_spec_item(get_memory_cell(1))) {
			print_str("Open Box: One of your keys unlocks the box. You open it.");
			i_am_unlocked = 1;
			play_sound(18);
			}

		}

	if (i_am_unlocked == 0) {
		i_am_unlocked = 0;
		if (get_stat_total(24) >= get_memory_cell(0)) {
			print_str("Open Box: You manage to pick the lock.");
			i_am_unlocked = 1;		
			play_sound(18);
			}
			else {
				if (get_memory_cell(0) >= 90) {
					print_str_color("Open Box: The box is locked. You don't have the key, and ",2);
					print_str_color("  the lock is too complicated to be picked.",2);
					}
					else if (get_memory_cell(1) > 0) {
						print_str("Open Box: The box is locked. You don't have the key, and your Tool Use skill");
						print_big_str("  isn't high enough to get it open. (Difficulty: ",get_memory_cell(0),")");
						}
						else {
							print_str("Open Box: The box is locked, and your Tool Use skill isn't high ");	
							print_big_str("  enough to get it open. (Difficulty: ",get_memory_cell(0),")");
							}
				}
		}
	
	if (i_am_unlocked == 0)
		end();
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
		
	if (i_am_open == 0) {
		i_am_open = 1;
		open_container();
		}
		else {
			close_container();
			i_am_open = 0;
			}
break;

beginstate UNLOCK_STATE;
	if (i_am_unlocked > 0) {
		print_str("Unlock Spell: The box is already unlocked.");
		end();
		}

	if (current_unlock_strength() >= get_memory_cell(0)) {
		print_str("Unlock Spell: The spell loosens the lock. The box is unlocked.");
		i_am_unlocked = 1;
		play_sound(18);
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		end();
		}
	print_str("Unlock Spell: The spell is too weak to affect the lock.");	
break;

